package org.example.booksmanagementsystem.mapper;

import org.apache.ibatis.annotations.*;
import org.example.booksmanagementsystem.model.BookInfo;
import org.example.booksmanagementsystem.model.PageRequest;

import java.util.List;

@Mapper
public interface SpecialDealNormalMapper {


    //从用户特价图书中查询图书信息
    @Select("select id,book_name,author,count,price,publish,status," +
            "create_time,update_time from #{userId}_special_book_info "+
            "where id = #{bookId} "
    )
    BookInfo queryBookInfoByNormalId(@Param("bookId") Integer bookId, @Param("userId") Integer userId);

    // 购买过图书，用户图书count+1
    @Update("update #{userId}_special_book_info set count=count+1 where id=#{bookId}")
    Integer addCountUserBookInfoTable(@Param("userId") Integer userId,@Param("bookId") Integer bookId);

    // 第一次购买图书，向用户图书表插入数据
    @Insert("Insert into #{userId}_special_book_info (id,book_name,author,count,price,publish,status) " +
            "values(#{bookInfo.id},#{bookInfo.bookName},#{bookInfo.author},1,#{bookInfo.price},#{bookInfo.publish},#{bookInfo.status})")
    Integer insertUserBookInfoTable(@Param("userId") Integer userId,@Param("bookInfo") BookInfo bookInfo);

    // 取消购买，用户特价表count-1
    @Update("update #{userId}_special_book_info set count=count-1 where id=#{bookId}")
    Integer deleteSpecialBookCount(@Param("bookId") Integer bookId, @Param("userId") Integer userId);

    // 查询用户特价表的书总数
    @Select("select count(*) from #{userId}_special_book_info ")
    Integer getAllSpecialShoppingtroll(Integer userId);

    // 分页查询 用户特价表的书
    @Select("select id,book_name,author,count,price,publish,status," +
            "create_time,update_time from #{userId}_special_book_info "+
            "where count !=0 and status !=0 order by id asc limit #{pageRequest.offset},#{pageRequest.pageSize} "
    )
    List<BookInfo> getSpecialShoppingtrollbyPage(@Param("pageRequest") PageRequest pageRequest,@Param("userId") Integer userId);
}
